package com.diyunapp.xilinapp.evaluations;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.adapters.EvaluationAdapter;
import com.diyunapp.xilinapp.bean.EvaluationBean;
import com.diyunapp.xilinapp.utils.FormatConfirm;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by NIDAYE on 2016/6/21.
 */
public class MiddleEvaluationFragment extends Fragment {

    @Bind(R.id.evaluation_lv)
    ListView evaluationLv;
    @Bind(R.id.no_evaluation_tv)
    TextView noEvaluationTv;
    private View myFragmentView;

    private List<EvaluationBean> myEvaluationList;
    private EvaluationAdapter myAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        myFragmentView = inflater.inflate(R.layout.evaluation_fragment, null);
        ButterKnife.bind(this, myFragmentView);

        myEvaluationList = new ArrayList<>();
        myAdapter = new EvaluationAdapter(myEvaluationList, getActivity());
        evaluationLv.setAdapter(myAdapter);

        getMiddleEvaluation();

        return myFragmentView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    private void getMiddleEvaluation() {
        String url = "http://www.xilingou.com/mobile/index.php?act=goods&op=goods_evaluate&goods_id=" + EvaluationActivity.good_id + "&type=zhong";
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(200);
        http.send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        int result = parseProject(responseInfo.result);
                        switch (result) {
                            case 1:
                                break;
                            case 2:
                                noEvaluationTv.setVisibility(View.VISIBLE);
                                evaluationLv.setVisibility(View.GONE);
                                break;
                            case 0:
                                Toast.makeText(getContext(), "查询失败！", Toast.LENGTH_LONG).show();
                                break;
                            default:
                                break;
                        }
                    }


                    @Override
                    public void onStart() {

                    }

                    private int parseProject(String result) {
                        Log.i("zhangxiaoxidebug", "detail_result=" + result);
                        try {
                            JSONObject jsObjectCode = new JSONObject(result);
                            int code = jsObjectCode.getInt("code");
                            if (code != 200) {
                                return 0;
                            }

                            JSONObject jsObjectDate = jsObjectCode.getJSONObject("date");
                            int num = FormatConfirm.stringToInt(jsObjectDate.getString("zhong"));
                            if(num<=0)
                            {
                                return  2;
                            }

                            noEvaluationTv.setVisibility(View.GONE);
                            evaluationLv.setVisibility(View.VISIBLE);
                            JSONArray jsArrayTotal = jsObjectDate.getJSONArray("zhongping");
                            for (int i = 0; i < jsArrayTotal.length(); i++) {
                                JSONObject jsObjectEva = jsArrayTotal.getJSONObject(i);
                                String geval_frommembername = jsObjectEva.getString("geval_frommembername");
                                String geval_scores = jsObjectEva.getString("geval_scores");
                                String geval_addtime = jsObjectEva.getString("geval_addtime");
                                String geval_content = jsObjectEva.getString("geval_content");
                                String geval_goodsname = jsObjectEva.getString("geval_goodsname");
                                String geval_explain = jsObjectEva.getString("geval_explain");
                                myEvaluationList.add(new EvaluationBean(geval_frommembername, geval_scores, geval_addtime, geval_content, geval_goodsname, geval_explain));
                            }
                            myAdapter.notifyDataSetChanged();
                        } catch (JSONException e) {
                            e.printStackTrace();
                            return 0;
                        }
                        return 1;
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        Log.i("zhangxiaoxidebug", "getIndex....onFailure msg=" + msg);
                        Toast.makeText(getContext(), "读取数据失败，请检查网络设置！", Toast.LENGTH_LONG).show();
                    }
                }
        );
    }
}
